// ActionScript file
import com.nstar.base.util.DateUtils;
import com.nstar.base.util.EffectUtils;
import com.nstar.base.util.Logger;
import com.nstar.base.util.SystemUtils;
import com.nstar.jianli.components.Toast;
import com.nstar.jianli.controller.WeiboController;
import com.nstar.jianli.itemrenderer.ResumeRenderer;
import com.nstar.jianli.vo.MicroBlogResumeVO;
import com.sina.microblog.events.MicroBlogEvent;

import flash.events.Event;

import mx.core.Application;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

public static const TRY_DESIGN:String = "tryDesign";
public static const MAX_CHILD:int = 5;
public static const IMAGE_SIZE:int = 180;
private var _controller:WeiboController;

private var logger:Logger = Logger.getInstance();

private var selectedIndex:int = -1;//当前页

private function init():void{
	//查询数据，获取已经发布的微博简历列表
	//queryListHandler();
	
	sampleData();
}

private function onComplete(e:Event):void{
	
}

private function sampleData():void{
	var result:String = "";
	for (var i:int =0 ;i<MAX_CHILD;i++){
		var row:String = "";
		row = "1,"+(1000+i)+","+Math.random()+",name"+i+",assets/avatar.jpg,安徽 合肥,http://weibo.com/statusUrl,高级软件工程师，交互设计,"+DateUtils.format(new Date(),"YYYY-MM-DD");
		result += row +";";
	}	
	
	result = result.substr(0,result.length-1);
	
	var list:Array = new Array();
	var rows:Array = result.split(";");
	
	for each(var rec:String in rows){
		var recs:Array = rec.split(",");
		ids.push(recs[1]);//id保存起来，用于查询转发和评论数
		var item:MicroBlogResumeVO = new MicroBlogResumeVO();
		item.id=recs[1];
		item.userId = recs[2];
		item.screenName = recs[3];
		item.profileImageUrl=recs[4];
		item.location = recs[5];;
		item.statusUrl=recs[6];
		item.text=recs[7];
		item.date=recs[8];
		
		item.repostsCount = 0;//转发数
		item.commentsCount = 0;//评论数
		list.push(item);
	}
	
	//resumeList.dataProvider = new ArrayCollection(list);
	
	startShowResumeHandler(list);
	
}

//首次只查询最新的20条
private function queryListHandler():void{
	var request:Object = new Object();
	queryList.send(request);
}

private var ids:Array = new Array();
private function queryListResultHandler(event:ResultEvent):void{
	if (event.result)
	{
		var list:Array = new Array();
		var result:String = event.result as String;
		var rows:Array = result.split(";");
		
		for each(var rec:String in rows){
			var recs:Array = rec.split(",");
			ids.push(recs[1]);//id保存起来，用于查询转发和评论数
			var item:MicroBlogResumeVO = new MicroBlogResumeVO();
			item.id=recs[1];
			item.userId = recs[2];
			item.screenName = recs[3];
			item.profileImageUrl=recs[4];
			item.statusUrl=recs[5];
			item.date=recs[6];
			
			item.repostsCount = 0;//转发数
			item.commentsCount = 0;//评论数
			list.push(item);
		}
		
		startShowResumeHandler(list);
	}
}

//简历显示处理
private function startShowResumeHandler(list:Array):void{
	for each(var vo:MicroBlogResumeVO in list){
		var renderer:ResumeRenderer = new ResumeRenderer();
		renderer.data = vo;
		renderer.addEventListener(ResumeRenderer.HIDE,countHandler);
		resumeList.addChild(renderer);
	}
	
	nextHandler();
	
}

var cnt:int = 0;
private function countHandler(event:Event):void{
	cnt++;
	if(cnt == MAX_CHILD){
		showHandler();
		cnt = 0;
	}
}

//下页数据
public function nextHandler():void{
	selectedIndex++;
	if(resumeList.numChildren>0){
		hideHandler();
	}else{
		showHandler();
	}
	
}

//隐藏显示的对象，在显示下一批数据时，先隐藏原来的显示对象
private function hideHandler():void{
	var app = Application.application;
	var idx:int = 0;
	for each(var item:Object in resumeList.getChildren()){
		if(item is ResumeRenderer){
			var x1:Number = (app.width)/2;
			var y1:Number = (app.height)/2;
			var delayTime:Number = (resumeList.numChildren-idx)*0.2;
			var endParams:Object = {x:x1,y:y1,width:0,height:0,alpha:0,delay:delayTime,time:0.3,onComplete:function(){item.hide();}};
			EffectUtils.playTween(item,null,endParams);
			idx++;
		}
	}
}

//显示对象
private function showHandler():void{
	//更新数据
	for each(var item:Object in resumeList.getChildren()){
		
	}
	
	resumeList.validateNow();
	var app = Application.application;
	
	var centerX:Number = (app.width-IMAGE_SIZE)/2;
	var centerY:Number = (app.height-IMAGE_SIZE)/2;
	
	var hudu:Number= SystemUtils.randRange(0,Math.PI*2/resumeList.numChildren );
	for(var i:int = resumeList.numChildren-1;i>=0;i--){
		var item:Object = resumeList.getChildAt(i);
		if(item is ResumeRenderer){
			var size:Number = 50+SystemUtils.randRange(0,IMAGE_SIZE-50);
			var r:Number = size+SystemUtils.randRange(0,50);
			
			var x1:Number = r*Math.cos(hudu+i*Math.PI*2/resumeList.numChildren);
			var y1:Number = r*Math.sin(hudu+i*Math.PI*2/resumeList.numChildren);
			
			var rr:Number = SystemUtils.randRange(0,360);
			var delayTime:Number = (resumeList.numChildren-i)*0.2;
			var startParams:Object = {width:400,height:400,alpha:0,rotation:rr};
			var endParams:Object = {delay:delayTime,time:1,x:centerX+x1,y:centerY+y1,width:size,height:size+20,alpha:1,rotation:0};
			EffectUtils.playTween(item,startParams,endParams);
		}
	}
}




//获取微博的转发和评论数完成
private function loadStatusCountsResultHandler(event:MicroBlogEvent):void{
	var list:Array = event.result as Array;
	//刷新列表数据，更新转发和评论数
	updateRepostCommentCount(list);
}

//更新数据
private function updateRepostCommentCount(list:Array):void{
//	for each(var item:Object in resumeList.dataProvider){
//		for each(var mc:MicroBlogCount in list){
//			if(item.id == mc.id){
//				item.repostsCount = mc.repostsCount;
//				item.commentsCount = mc.commentsCount;
//				ArrayCollection(resumeList.dataProvider).refresh();
//				break;
//			}
//		}
//	}
}

private function faultHandler(event:FaultEvent):void
{
	Toast.show("query error =>"+event.message);
}

public function get controller():WeiboController{
	return this._controller;
}

public function set controller(value:WeiboController):void{
	this._controller = value;
	if(_controller.hasEventListener(MicroBlogEvent.LOAD_STATUS_COUNTS_RESULT)==false)
		_controller.addEventListener(MicroBlogEvent.LOAD_STATUS_COUNTS_RESULT,loadStatusCountsResultHandler);
}

private function tryHandler():void{
	this.dispatchEvent(new Event(TRY_DESIGN));
}
